<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop">
          <div style="width:100%;">
              <el-col>
                  <div ref="buttondiv">
                      <vxe-toolbar>
                          <template v-slot:buttons>
                              <table>
                                  <tr>
                                      <td>报告单元</td>
                                      <td>
                                          <rm-ddl-rptunit prgno="LIS335" v-model="rptunit" :appendtobody="true" width="200px" :defaultflag="'1'" :clearable="false" @change="conditionChange"></rm-ddl-rptunit>
                                      </td>
                                      <td>日期</td>
                                      <td>
                                          <el-date-picker v-model="selectdate" value-format="yyyy-MM-dd" :clearable="false" style="width:130px" ref="specdate" @change="conditionChange"></el-date-picker>
                                      </td>
                                      <td>审核者</td>
                                      <td>
                                          <rm-ddl-common v-model="chkuser" datatype="userinfo" dataname="审核者" width="100px" @change="conditionChange"></rm-ddl-common>
                                      </td>
                                      <td>发布状态</td>
                                      <td>
                                          <el-select v-model="sendflag" style="width:80px" @change="conditionChange">
                                              <!-- <el-option value="0" label="全部"></el-option> -->
                                              <el-option value="1" label="未发布"></el-option>
                                              <el-option value="2" label="已发布"></el-option>
                                          </el-select>
                                      </td>
                                      <td>
                                          当前混样报告共<span style="padding-left: 3px ;">{{ tableDataMain.length }}</span> 条,发布成功数量<span style="color: blue;padding-left: 3px ;">{{ successCount }}</span> 条, 发布失败数量<span style="color: red;padding-left: 3px ;">{{ failCount }} </span>条
                                      </td>
                                      <td :width="buttonwidth"></td>
                                      <td>
                                          <el-button @click="QueryData()" icon="el-icon-search">查询</el-button>
                                      </td>
                                      <td>
                                          <el-button v-if="sendflag==1" @click="BatchSendTotal()" icon="el-icon-upload2">批次发布</el-button>
                                      </td>
                                  </tr>
                              </table>
                          </template>
                      </vxe-toolbar>
                  </div>
              </el-col>
          </div>

      </div>
      <div class="pagecenter">
          <rm-layout-right splitcolor="#ffffff" :splitwidth="3" :fixsize="600" :minfixsize="200" ref="layoutright">
              <div style="height: 100%">
                  <vxe-table :data.sync="tableDataMain" :loading="loading" :select-config="{ checkAll: true }" @current-change="currentChangeEvent" :cell-class-name="cellClassName" height="100%" highlight-current-row highlight-hover-row resizable show-overflow="tooltip" show-header-overflow="tooltip" header-align="center" size="mini" border stripe column-width="100px" ref="tableMix">
                      <vxe-table-column type="selection" width="40px" align="center"></vxe-table-column>
                      <vxe-table-column field="sampleno" title="标本号" width="46px" align="center" sortable></vxe-table-column>
                      <!-- <vxe-table-column field="mixtype" title="混样类型" width="80px" align="center"></vxe-table-column> -->
                      <vxe-table-column field="cp_msg" title="发布消息提示" width="200px"></vxe-table-column>
                      <vxe-table-column field="rechk_user" title="审核人" width="80px" align="center">
                      </vxe-table-column>
                      <vxe-table-column field="rechk_dt" title="审核时间" width="120px" sortable></vxe-table-column>
                      <vxe-table-column field="mixnum" title="包含标本数" width="80px" align="center"></vxe-table-column>
                      <vxe-table-column field="mixbarcode" title="混样条码号" width="100px" sortable></vxe-table-column>
                      <vxe-table-column field="mix_dt" title="采样时间" width="120px" sortable></vxe-table-column>
                      <vxe-table-column field="mix_user" title="采样人" width="80px" align="center"></vxe-table-column>
                      <vxe-table-column field="reportid" title="报告ID" width="100px"></vxe-table-column>
                      <vxe-table-column field="sampledate" title="标本日期" width="80px" align="center" sortable></vxe-table-column>
                      <vxe-table-column field="rptunitname" title="报告单元" width="120px" align="center" sortable></vxe-table-column>
                      <!-- 加后台返回的消息 -->
                  </vxe-table>
              </div>
              <template v-slot:fix>
                  <div style="height: 100%">
                      <div style="height: 30%">
                          <vxe-table :data="tableDataResult" height="100%" highlight-current-row :loading="loadingResult" highlight-hover-row resizable show-overflow="tooltip" show-header-overflow="tooltip" border size="mini" stripe header-align="center" column-width="100px" ref="tableMdl">
                              <vxe-table-column field="rpt_itemname" sortable title="检验项目" width="220" show-overflow></vxe-table-column>
                              <vxe-table-column field="result_str" sortable title="检测结果" width="80" show-overflow></vxe-table-column>
                          </vxe-table>
                      </div>
                      <div style="height:calc(70% - 5px);margin-top:5px">
                          <vxe-table :data.sync="tableDataDetail" column-width="100px" height="100%" :loading="loadingDetail" highlight-current-row highlight-hover-row resizable border header-align="center" show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe>
                              <vxe-table-column field="barcode" title="条码号" width="100px"></vxe-table-column>

                              <vxe-table-column field="pat_name" title="姓名" width="70px" align="center" sortable></vxe-table-column>
                              <vxe-table-column field="req_statname" title="状态" width="60px" align="center" sortable></vxe-table-column>
                              <vxe-table-column field="sample_items" title="申请项目" width="160px" sortable></vxe-table-column>
                              <vxe-table-column field="sampleno" title="标本号" width="100px" sort-width="20" sortable></vxe-table-column>
                              <vxe-table-column field="sampledate" title="标本日期" width="80px" align="center" sortable></vxe-table-column>
                              <vxe-table-column field="rptunitname" title="报告单元" width="120px" align="center" sortable></vxe-table-column>
                              <vxe-table-column field="rechk_username" title="审核人" width="80px" align="center">
                              </vxe-table-column>
                              <vxe-table-column field="rechk_dt" title="审核时间" width="120px" sortable></vxe-table-column>
                              <vxe-table-column field="pat_no" title="病历号" width="90px" sortable></vxe-table-column>
                              <vxe-table-column field="pat_sexname" title="性别" width="30px" align="center" sortable></vxe-table-column>
                              <vxe-table-column field="pat_agestr" title="年龄" width="50px" align="center" sortable></vxe-table-column>
                              <vxe-table-column field="sampled_dt" title="采样时间" width="120px" align="center"></vxe-table-column>
                              <vxe-table-column field="sampled_user" title="采样人" width="60px" align="center"></vxe-table-column>
                              <vxe-table-column field="pat_idcardno" title="身份证号" width="100px"></vxe-table-column>
                              <vxe-table-column field="pat_phone" title="联系电话" width="100px"></vxe-table-column>
                          </vxe-table>
                      </div>
                  </div>
              </template>
          </rm-layout-right>

      </div>

  </div>
</template>

<script>
export default {
  name: "LIS335",
  data() {
      return {
          rptunit: "",
          selectdate: "",
          chkuser: "",
          tableDataMain: [],
          tableDataResult: [],
          tableDataDetail: [],
          buttonwidth: "2000px",
          loading: false,
          loadingResult: false,
          loadingDetail: false,
          sendflag: "1", //发布状态
          successCount: 0, //发布成功条数
          failCount: 0, //发布失败条数
          dataCount: 0,
          SendData: []
      };
  },
  created() {
      this.InitData();
  },
  mounted() {},
  updated() {},
  methods: {
      InitData() {
          var unitid = this.$route.query.rptunitid;
          var chkuser = this.$route.query.chkuser;
          if (unitid) this.rptunit = unitid;
          if (chkuser) this.chkuser = chkuser;
          else this.chkuser = this.$tool.getSession(this.$tool.loginuserno);
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS335",
                  method: "post",
                  data: {
                      action: "InitPageDate"
                  }
              })
              .then(res => {
                  if (res && !res.hasOwnProperty("errcode")) {
                      this.selectdate = res.date;
                  }
              });
      },
      QueryData() {
          this.loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS335",
                  method: "post",
                  data: {
                      action: "Query",
                      rptunitid: this.rptunit,
                      datetime: this.selectdate,
                      chkuser: this.chkuser,
                      sendflag: this.sendflag
                  }
              })
              .then(res => {
                  if (res && res.errcode == "1") {
                      this.tableDataMain = res.Data;
                      this.successCount = 0;
                      this.failCount = 0;
                      if (this.tableDataMain.length > 0) {
                          this.QueryResult(this.tableDataMain[0].reportid);
                          this.QueryDetail(this.tableDataMain[0].mixbarcode);
                      }
                  } else {
                      this.$alert(res.errmsg, "提示");
                      this.tableDataMain = [];
                  }
                  this.loading = false;
              })
              .catch(err => {
                  this.loading = false;
              });
      },
      QueryResult(reportid) {
          this.loadingResult = true;
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS335",
                  method: "post",
                  data: {
                      action: "QueryResult",
                      rptunitid: this.rptunit,
                      reportid: reportid
                  }
              })
              .then(res => {
                  if (res && res.errcode == "1") {
                      this.tableDataResult = res.Data;
                  } else {
                      this.$alert(res.errmsg, "提示");
                      this.tableDataResult = [];
                  }
                  this.loadingResult = false;
              })
              .catch(err => {
                  this.loadingResult = false;
              });
      },
      QueryDetail(mixbarcode) {
          this.loadingDetail = true;
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS335",
                  method: "post",
                  data: {
                      action: "QueryDetail",
                      mixbarcode: mixbarcode
                  }
              })
              .then(res => {
                  if (res && res.errcode == "1") {
                      this.tableDataDetail = res.Data;
                  } else {
                      this.$alert(res.errmsg, "提示");
                      this.tableDataDetail = [];
                  }
                  this.loadingDetail = false;
              })
              .catch(err => {
                  this.loadingDetail = false;
              });
      },
      currentChangeEvent({ row }) {
          this.QueryResult(row.reportid);
          this.QueryDetail(row.mixbarcode);
      },
      conditionChange() {
          this.tableDataMain = [];
          this.tableDataDetail = [];
          this.tableDataResult = [];
      },
      BatchSendTotal() {
          var datas = this.$refs.tableMix.getSelectRecords();
          this.SendData = datas.filter(item => item.cp_flag != "1");
          if (this.SendData.length > 0) {
              this.dataCount = 0;
              this.successCount = 0;
              this.failCount = 0;
              this.BatchSend(this.SendData[0]);
          }
      },
      BatchSend(row) {
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS335",
                  method: "post",
                  data: {
                      action: "BatchSend",
                      mixbarcode: row.mixbarcode,
                      reportid: row.reportid
                  }
              })
              .then(res => {
                  if (res.flag == "1") {
                      row.cp_flag = "1";
                      row.cp_msg = "发布成功";
                      this.successCount++;
                  } else {
                      row.cp_flag = res.flag;
                      // if (res.msg.errorMsg.length > 0) {
                      //     row.cp_msg = res.msg.errorMsg[0];
                      // } else {
                      //     row.cp_msg = "发布失败";
                      // }
                      if (res.msg) {
                          row.cp_msg = res.msg;
                      } else {
                          row.cp_msg = "发布失败";
                      }
                      this.failCount++;
                  }
                  if (this.dataCount++ < this.SendData.length) {
                      this.BatchSend(this.SendData[this.dataCount]);
                  }
              })
              .catch(err => {});
      },
      cellClassName({ row, column }) {
          if (column.property == "cp_msg") {
              if (row.cp_flag != "") {
                  if (row.cp_flag == "1") {
                      return "col-green";
                  } else {
                      return "col-red";
                  }
              }
          }
      }
  }
};
</script>

<style scoped>
.pagetop {
  left: 0;
  right: 0;
  height: 36px;
  display: -webkit-box;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  border: solid 1px #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 5px;
}

table tr td:nth-child(odd) {
  text-align: right;
  white-space: nowrap;
}
table tr td:nth-child(even) {
  text-align: left;
}
table tr td:nth-child(1) {
  width: 80px;
}
table {
  font-size: 12px;
  border-collapse: separate;
  border-spacing: 5px;
}
/deep/ .vxe-table .vxe-body--column.col-red {
  color: #f56c6c;
}
/deep/ .vxe-table .vxe-body--column.col-yellow {
  color: #999900;
}
/deep/ .vxe-table .vxe-body--column.col-green {
  color: #67c23a;
}
</style>